<div class="row animate fadeIn">
    <form id="addLookUpForm" class="form-horizontal"  method="post">
        <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Add Module</i></h5>
            <div class="ibox-tools">
                <button type="button" id="saveLookUp" class="btn btn-sm btn-primary save-btn-tab">Save</button>
                <a id="cancel_add" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Cancel</a>
            </div>
        </div>
        <div class="ibox-content">
            <div class="form-group"><label class="col-lg-3 control-label">Code</label>
                <div class="col-lg-10 input-s">
                    <input type="text" name="lookup_type" class="form-control">
                </div>
            </div>
            <div class="form-group"><label class="col-lg-3 control-label">Description</label>
                <div class="col-lg-11 input-s">
                    <input type="text" name="lookup_desc" class="form-control">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Enabled</label>
                <div class="col-lg-10 input-s">
                    <input type="checkbox" name="enableBox" value="ok" class="i-checks" checked>
                </div>
            </div>
    </form>
        </div>
         <div id="add-value" style="display:none;">
           <div class="ibox-title no-top-border">
            <button class="btn btn-success m-t-n-xs btn-md add-tab-btn" data-toggle="modal" data-target="#addNewa">New</button>
          </div>
          <div class="ibox-content">
            <table class="table table-bordered">
                <thead>
                  <tr>
                    <th>Code</th>
                    <th>Meaning</th>
                    <th>Description</th>
                    <th>Enable Flag</th>
                  </tr>
                </thead>
                <tbody id="luva">
                  
                </tbody>
            </table>
        </div>
         </div>
</div>

<div class="modal fade" id="addNewa" tabindex="-1" role="dialog">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
           <h4 class="modal-title" id="myModalLabel">Edit Look Up Values</h4>
        </div>
      <div class="modal-body">
        <div class="row">
          <form id="luvFormAdda" method="post" class="form-horizontal">
              <input type="hidden" name="LookUpType" value="">
              <div class="form-group">
                  <label class="col-lg-3 control-label">Code</label>
                  <div class="col-lg-5">
                      <input type="text" name="LookUpCode" class="form-control" value="">
                  </div>
              </div>
              <div class="form-group">
                  <label class="col-lg-3 control-label">Meaning</label>
                  <div class="col-lg-5">
                      <input type="text" name="LookUpMeaning" class="form-control" value="">
                  </div>
              </div>
              <div class="form-group">
                  <label class="col-lg-3 control-label">Description</label>
                  <div class="col-lg-5">
                      <input type="text" name="LookUpDesc" class="form-control" value="">
                  </div>
              </div>
              <div class="form-group">
                  <label class="col-lg-3 control-label">Enable FLag</label>
                  <div class="col-lg-5">
                      <input type="checkbox" value="ok" checked name="LookUpEF" class="i-checks">
                  </div>
              </div>
          </form>
        </div>
      </div>
      <div class="modal-footer">
           <button id="store_lookupvaluesa" type="button" class="btn btn-primary" >Save</button>
      </div>
    </div>
  </div>
</div> <!-- end-of-modal -->
      <script>
            $(document).ready(function () {
              $('.i-checks').iCheck({
                    checkboxClass: 'icheckbox_square-green',
                    radioClass: 'iradio_square-green',
              });

               $("#addLookUpForm").validate({
                rules:{
                    lookup_type: {
                        required: true
                    },
                    lookup_desc: {
                        required: true
                    }
                  }
                });

               $("#saveLookUp").click(function(){
                 if($("#addLookUpForm").valid() == true ){
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax').'/insertLookUp'); ?>",
                        data: $('#addLookUpForm').serialize(),
                        dataType: 'json',
                        success: function(data){
                          if(data.err == 1){
                            $("#saveLookUp").removeAttr('disabled');
                            notifymsg("danger",data.msg);
                          }else{
                            notifymsg("success",". . .Look Up  Added Successfully");
                            $("#addLookUpForm input[name=lookup_type]").attr('disabled',true);
                            $("#luvFormAdda input[name=LookUpType]").val(data.code);
                            $('#add-value').show();
                          }
                        },
                        beforeSend: function(){
                           $("#saveLookUp").attr('disabled',true);
                        }
                    });
                 }
               });

               $("#luvFormAdda").validate({
                     rules: {
                         LookUpCode: {
                             required: true
                         }
                     }
                });

               //getLookUpValuesa();
                function getLookUpValuesa(){
                  var type = $('#luvFormAdda input[name=LookUpType]').val();
                  $.ajax({
                    type: "POST",
                    url: "<?php echo base_url($this->session->userdata('forajax') . '/getLookUpValues'); ?>",
                    data: {'lookuptype': type },
                    dataType: 'json',
                    success: function(data){
                        var tmp = '';
                        $.each(data, function(i,item) {
                          tmp += "<tr>" 
                              + "<td class='type' style='display: none;'>" + data[i].lookup_type + "</td>"
                              + "<td class='code'>" + data[i].lookup_code + "</td>"
                              + "<td class='meaning'>" + data[i].meaning + "</td>"
                              + "<td class='description'>" + data[i].description + "</td>"
                              + "<td class='enable_flag' style='display: none;'>" + data[i].enable_flag + "</td>"
                              + '<td align="center" class=""><div class="state icheckbox_square-green '+ (data[i].enable_flag == 'Y' ? 'checked' : 'normal') +'"></div></td>'
                              + "</tr>";
                        });

                        $('#luva').html(tmp);
                    }
                  });
                }

              $(document).on('click',"#store_lookupvaluesa",function(){
              if($("#luvFormAdda").valid() == true){
                $.ajax({
                  type: "POST",
                  url: "<?php echo base_url($this->session->userdata('forajax') . '/insertLookUpValues'); ?>",
                  data: $("#luvFormAdda").serialize(),
                  dataType: 'json',
                  success: function(data){
                    if(data.err == 1){
                      getLookUpValuesa();
                      $('#store_lookupvaluesa').removeAttr('disabled');
                      $('#addNewa').modal('hide');
                      notifymsg("danger",data.msg);
                    }else{
                      getLookUpValuesa();
                      $('#addNewa').modal('hide');
                      notifymsg("success"," . . .Add Succesful");
                      $("#luvFormAdda")[0].reset();
                      $('#store_lookupvaluesa').removeAttr('disabled');
                    }
                  },
                  beforeSend: function(){
                    $('#store_lookupvaluesa').attr('disabled',true);
                  }
                });
              }
            });

                  $('#cancel_add').click(function () {
                              swal({
                                          title: "Are you sure?",
                                          text: "Your work will not be save",
                                          type: "warning",
                                          showCancelButton: true,
                                          confirmButtonColor: "#DD6B55",
                                          confirmButtonText: "Leave",
                                          cancelButtonText: "Stay",
                                          closeOnConfirm: true,
                                          closeOnCancel: true },
                                      function (isConfirm) {
                                          if (isConfirm) {
                                            swal("success");
                                              close_add(); active_search();
                                              notifymsg("hide","hide");
                                          } else {
                                              swal("Cancelled");
                                          }
                                      });
                          });

            });
        $('#addX').click(function(){
            close_add();
       });
</script>